<?php
$startTime =  microtime(true);

require_once("../Lib/psiframework.php");
import("Classes.Util.Database.Database");

$fileContent = file("./pgsql.sql");
$queries = "" ;
foreach ($fileContent as $sqlLine) {
    $sqlLineTrimmed = trim($sqlLine);
    if ($sqlLineTrimmed !== "" && strpos($sqlLineTrimmed, "--") !== 0) {
        $queries .= $sqlLineTrimmed . " " ;
    }
}
 
$q = preg_split("[;]", $queries);
 
$database = Database::getInstance();
$database->startTransaction();

$rollBack = false ;
 
foreach ($q as $query) {
    if ($query != null && trim($query) != "") {
        $ok = $database->execute($query);
        if (!$ok) {
            $rollBack = true ;
            break;
        }
    }
}
 
$endTime = microtime(true);
$duration = $endTime - $startTime ;
 
if ($rollBack) {
    echo "ECHEC" ;
    $database->rollback();
} else {
    echo "REUSSI" ;
    $database->commit();
}
 
echo " en " . $duration . "s." ;
 